package com.boot.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.boot.web.dao.GoodsSpecAttributeDao;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.GoodsSpecAttribute;
import com.boot.web.service.GoodsSpecAttributeService;

/**
 * 商品规格属性控制器
 * @author 【zhouJie】
 * @date 2021-09-28 17:53:49
 */
@RestController
@RequestMapping("/goodsSpecAttribute")
public class GoodsSpecAttributeController {

    @Autowired
    private GoodsSpecAttributeService goodsSpecAttributeService;

    @Autowired
    private GoodsSpecAttributeDao goodsSpecAttributeDao;

    @PostMapping("/save")
    public ResponseServer save(GoodsSpecAttribute specAttribute) {
        specAttribute.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", goodsSpecAttributeService.save(specAttribute));
    }

    @PutMapping("/update")
    public ResponseServer update(GoodsSpecAttribute specAttribute) {
        specAttribute.setUpdateTime(new Date());
        if("".equals(specAttribute.getAttributeName())){
            GoodsSpecAttribute gsa = goodsSpecAttributeDao.queryById(specAttribute.getId());
            if(gsa.getCategoryId() == specAttribute.getCategoryId()){
                goodsSpecAttributeService.update(specAttribute);
            }else{
                goodsSpecAttributeService.update(specAttribute);
                List<GoodsSpecAttribute> gsaList = goodsSpecAttributeDao.queryGoodsSpecAttributeByParentId(specAttribute.getId());
                if(gsaList.size() > 0){
                    for(GoodsSpecAttribute spec : gsaList){
                        spec.setAddId(specAttribute.getCategoryId());
                        goodsSpecAttributeService.update(spec);
                    }
                }
            }
        }else{
            if(specAttribute.getCategoryId() == null){
                goodsSpecAttributeService.update(specAttribute);
            }else{
                GoodsSpecAttribute newSa = new GoodsSpecAttribute();
                newSa.setAttributeName(specAttribute.getAttributeName());
                newSa.setParentId(specAttribute.getId());
                newSa.setAddId(specAttribute.getCategoryId());
                newSa.setUpdateTime(new Date());
                goodsSpecAttributeService.save(newSa);
            }
        }
        return ResponseServer.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        List<GoodsSpecAttribute> list = goodsSpecAttributeDao.queryGoodsSpecAttributeByParentId(id);
        if(list.size() > 0){
            for(int i = 0; i < list.size(); i++){
                goodsSpecAttributeDao.delete(list.get(i).getId());
            }
        }
        return ResponseServer.success("删除成功", goodsSpecAttributeService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        for(int j = 0; j < ids.length; j++){
            List<GoodsSpecAttribute> list = goodsSpecAttributeDao.queryGoodsSpecAttributeByParentId(ids[j]);
            if(list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    goodsSpecAttributeService.delete(list.get(i).getId());
                }
                goodsSpecAttributeService.delete(ids[j]);
            }else{
                goodsSpecAttributeService.delete(ids[j]);
            }
        }
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(goodsSpecAttributeService.getGoodsSpecAttributeVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(goodsSpecAttributeService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(goodsSpecAttributeService.getById(id));
    }

}
